// components/login/index.js

  const config = require("../../config/config");
  const app = getApp();
  const {checkLogin}=require('../../utils/util')
  const {
    wxLogin,decodeUserInfo
  } = require('../../api/login')
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        loginVisible:{
            type:Boolean,
            value:true
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
      code:''
    },

    /**
     * 组件的方法列表
     */
    observers: {
      'loginVisible': function (selectIndex) {
        // 在 rate被设置时，执行这个函数
        

      }
    },
    methods: {
      hideModal(){
        this.setData({
          loginVisible:true
        })
      },
      getUserProfile(e) {
        var that = this
        this.hideModal()
        wx.showLoading({
          title: '登录中',
        })
        wx.getUserProfile({
          desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
          success: async function (res) {
            const {
              encryptedData,
              iv,
            } = res;
            const info = res.userInfo
            wx.login({
              success: async function (ress) {
                const param = {
                  avatarUrl: info.avatarUrl,
                  city: info.city,
                  code: ress.code,
                  country: info.city,
                  encryptedData,
                  iv,
                  language: info.language,
                  merchantId: 0,
                  nickName: info.nickName,
                  promoterId: 0,
                  province: info.province,
                  sex: info.gender,
                  promoteUser: wx.getStorageSync('promoteUser'),
                }
    
                const {
                  result
                } = await wxLogin(param);
                const {userInfo,token}=result
                console.log(userInfo, token)
                let newTime = Math.round(new Date())
                wx.setStorageSync(config.storageKey.userId, userInfo.id);
                wx.setStorageSync(config.storageKey.loginTime, newTime);
                wx.setStorageSync(config.storageKey.token, token)
                that.fetchUserInfo()
                wx.hideLoading()
                that.setData({
                  userInfo,
                  isLogin: true
                })
                that.triggerEvent('onloadFunc')
                if(!userInfo.mobile==''){
                  wx.setStorageSync(config.storageKey.mobile, userInfo.mobile)
                }
              },fail:(e)=>{
                wx.hideLoading()
              }
            })
    
          }, fail: (e) => {
            wx.hideLoading()
          }
        })
    
      },
      async fetchUserInfo() {
        const {
          result
        } = await decodeUserInfo()
        wx.setStorageSync(config.storageKey.userInfo, result);
      },
    }
})
